package com.gongguizhijia.Util;

import com.alibaba.excel.EasyExcel;
import com.gongguizhijia.Constants.Constants;
import com.gongguizhijia.listen.CommonListen;
import com.gongguizhijia.pojo.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Chenglei
 * @date 2021/6/2 9:06
 */
public class ImportExcel {

    /**
     * 导入Excel数据
     */
    public static Map<String, List> importExcel() {

        Map<String, List> map = new HashMap<>(16);

        //excel路径
        String filePath1 = "src\\file\\menu.xlsx";
        String filePath2 = "src\\file\\DTC.xlsx";
        String filePath3 = "src\\file\\DataFlow.xlsx";
        String filePath4 = "src\\file\\EcuDataFlowConfig.xlsx";
        String filePath5 = "src\\file\\system_map_mdb.xlsx";
        String filePath6 = "src\\file\\ECU.xlsx";
        String filePath7 = "src\\file\\EcuInfoParseConfig.xlsx";

        map.put(Constants.MENU, getExcelList(filePath1, Menu.class));
        map.put(Constants.DTC, getExcelList(filePath2, DTC.class));
        map.put(Constants.DATA_FLOW, getExcelList(filePath3, DataFlow.class));
        map.put(Constants.ECU_DATA_FLOW_CONFIG, getExcelList(filePath4, EcuDataFlowConfig.class));
        map.put(Constants.SYSTEM_MAP_MDB, getExcelList(filePath5, SystemMapMdb.class));
        map.put(Constants.ECU, getExcelList(filePath6, ECU.class));
        map.put(Constants.ECU_INFO_PARSE_CONFIG, getExcelList(filePath7, EcuInfoParseConfig.class));

        //返回数据
        return map;
    }


    /**
     * 获取excel内容list
     *
     * @param filePath
     * @param clazz
     * @return
     */
    public static List<?> getExcelList(String filePath, Class<?> clazz) {

        File file = new File(filePath);
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        CommonListen<?> commonListen = new CommonListen<>(clazz);
        EasyExcel.read(fileInputStream, clazz, commonListen).sheet().doRead();
        List<?> excelList = commonListen.getList();
        return excelList;
    }
}
